//
//  MainClearVC.m
//  NavClearDemo
//
//  Created by LH-mac on 2019/5/30.
//  Copyright © 2019年 LH-mac. All rights reserved.
//

#import "MainClearVC.h"
#import "NavClearView.h"
#import "UIImage+Color.h"
#define kStatusBarHeight [[UIApplication sharedApplication] statusBarFrame].size.height //状态栏高度
#define kNavigationBarHeight (kStatusBarHeight + 44) //整个导航栏高度
#define RGBAColor(r,g,b,a) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:a]
@interface MainClearVC ()<scrollDelegate>
@property (assign, nonatomic) CGFloat alpha;
@end

@implementation MainClearVC

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self setupNavBackGround:_alpha];//恢复此界面消失时的导航栏透明度
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self setupNavBackGround:1];//不影响下一个界面的导航栏
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NavClearView *view = [NavClearView getInstanceNibName:@"NavClearView"];
    view.delegate = self;
    view.frame = self.view.bounds;
    [self.view addSubview:view];
    
    if (@available(iOS 11.0, *)) {
        view.tb.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
}

- (void)scrollToGetY:(CGFloat)y{
    //透明度
    //找最大值/
    _alpha = y * 1 / (200-kNavigationBarHeight); //（200这个数字自己设定）表示透明度为最大时的临界距离
    if (_alpha >= 1) {
        // 透明度最大值
        _alpha = 0.99;
    }
    [self setupNavBackGround:_alpha];
}

- (void)setupNavBackGround:(CGFloat)alpha{
    //把颜色生成图片
    UIColor *alphaColor = RGBAColor(231, 31, 17, alpha);
    UIImage *alphaImage = [UIImage imageWithColor:alphaColor];
    //修改导航条背景图片
    [self.navigationController.navigationBar setBackgroundImage:alphaImage forBarMetrics:UIBarMetricsDefault];
    //导航栏线条设置成和导航栏背景一样可实现伪隐藏
    [self.navigationController.navigationBar setShadowImage:alphaImage];
    
    if (alpha > 0.2) {
        self.navigationItem.title = @"首页";
    }else{
        self.navigationItem.title = @"";
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
